package nxu.it.controller;

import io.jooby.Context;
import io.jooby.MediaType;
import io.jooby.ModelAndView;
import io.jooby.annotation.*;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import nxu.it.model.*;
import nxu.it.service.GreetingService;

import java.util.*;

@Path("/")
@Singleton
public class MainController {
    GreetingService greetingService;

    @Inject
    public MainController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @GET(value = "/hello", produces = MediaType.HTML)
    public String hello(@QueryParam String username,@QueryParam  int gender) {
        return String.format("你好，%s%s",(username==null)? "无名氏" : username, (gender == 1)?"先生":"女士");
    }

    @GET(value="/login",produces = MediaType.HTML)
    public ModelAndView login(){
        Map<String, Object> data=new HashMap<>();
        return new ModelAndView("login.ftl",data);
    }

    @POST(value="/loginCheck",produces = MediaType.HTML)
    public String loginCheck(@FormParam Loginrequest loginrequest) {
        if(loginrequest.getUsername()==null||loginrequest.getPassword()==null){
            return "用户名或密码不能为空";
        }else if(loginrequest.getUsername().equals(loginrequest.getPassword())){
            return "登录成功";
        }else {
            return "用户名或密码错误";
        }
    }
    @GET(value="/register",produces = MediaType.HTML)
    public ModelAndView register(){
        Map<String, Object> data=new HashMap<>();
        List<Hometown> hometownList= new ArrayList<>();
        hometownList.add(new Hometown("ningxia","宁夏"));
        hometownList.add(new Hometown("shannxi","陕西"));
        hometownList.add(new Hometown("gansu","甘肃"));
        hometownList.add(new Hometown("other","其他"));
        data.put("hometownList",hometownList);
        List<Hobby> hobbyList= new ArrayList<>();
        hobbyList.add(new Hobby("sing","唱歌"));
        hobbyList.add(new Hobby("dance","跳舞"));
        hobbyList.add(new Hobby("lanqiu","篮球"));
        hobbyList.add(new Hobby("rap","说唱"));
        data.put("hobbyList",hobbyList);
        return new ModelAndView("register.ftl",data);
    }
    @POST(value="/doRegister",produces = MediaType.HTML)
    public String doRegister(@FormParam Registerrequest registerrequest) {
        return registerrequest !=null ? registerrequest.toString():"";
    }
    @GET(value = "/today", produces =MediaType.JSON)
    public String today() {
        // 获取当前日期和时间
        Calendar calendar = Calendar.getInstance();
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

        int year = calendar.get(Calendar.YEAR);

        int month = calendar.get(Calendar.MONTH);

        int date = calendar.get(Calendar.DATE);

        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        int minute = calendar.get(Calendar.MINUTE);

        String weekday = weekDays[calendar.get(Calendar.DAY_OF_WEEK)-1];
        return String.format("year: %d, month: %d, day: %d, hour: %d, minute: %d, weekday: %s",year,month,date,hour,minute,weekday);
    }
}
